<template>
  <BasicModal v-bind="$attrs" @register="userRegister" :title="getTitle" width="900px" @ok="submitAudit">
    <BasicTable @register="registerUserTable" :loading="loading" :rowSelection="{ type: 'Checkbox' }"
      :pagination="false">
    </BasicTable>
  </BasicModal>
</template>

<script lang="ts" setup>
import { BasicModal, useModalInner } from '/@/components/Modal';
import { ref, reactive } from 'vue';
import { useListPage } from '/@/hooks/system/useListPage';
import { handleUsersTwo } from './LabEntrustAllotList.api';
import { labUserColumnsB } from './LabEntrustAllotList.data';
import { BasicTable } from '/@/components/Table';
import { FormSchema } from '/@/components/Table';
const loading = ref(false);
const getTitle = ref('可选实验人员');
const formData = reactive({
  itemId: '',
  basisId: '',
  userName: '',
});
const emits = defineEmits<{
  (event: 'getSelectUserSkill', uId: String, uName: String): void;
}>();

const [userRegister, { setModalProps, closeModal }] = useModalInner(async (data) => {
  setModalProps({
    minHeight: 180,
  });
  formData.itemId = data.itemId;
  formData.basisId = data.basisId;
  reload();
});
const searchFormSchema: FormSchema[] = [
  {
    label: '姓名',
    field: "formData.userName",
    component: 'Input',
    colProps: { span: 6 },
  },
];
//注册table数据
const { tableContext } = useListPage({
  tableProps: {
    api: handleUsersTwo,
    columns: labUserColumnsB,
    canResize: false,
    formConfig: {
      schemas: searchFormSchema,
    },
    showIndexColumn: true,
    showTableSetting: false,
    showActionColumn: false,
    beforeFetch: (params) => {
      params.pageSize = 100;
      if (params.formData) {
        params.userName = params.formData.userName;
        return Object.assign(params, { itemId: formData.itemId, basisId: data.basisId, userName: params.userName });
      } else {
        return Object.assign(params, { itemId: formData.itemId, basisId: data.basisId });
      };
    },
  },
});

const [registerUserTable, { reload, getSelectRows }] = tableContext;

async function submitAudit() {
  let ids = "";
  let names = "";
  const selectedRows = getSelectRows();
  selectedRows.forEach((row) => {
    const id = row['userId'];
    const userName = row['userName'];
    if (ids == "") {
      ids = id;
      names = userName;
    } else {
      ids = ids + ',' + id;
      names = names + ',' + userName;
    }
  })
  emits('getSelectUserSkill', ids, names);

  closeModal();
}
</script>
